package cn.thr.pethome.shop.service.impl;

import cn.thr.pethome.jwt.LoginContextUtils;
import cn.thr.pethome.shop.domain.Shop;
import cn.thr.pethome.shop.domain.ShopAuditLog;
import cn.thr.pethome.shop.mapper.ShopMapper;
import cn.thr.pethome.shop.service.IShopAuditLogService;
import cn.thr.pethome.base.service.Impl.BaseServiceImpl;
import cn.thr.pethome.util.EmailUtil;
import cn.thr.pethome.user.domain.Employee;
import cn.thr.pethome.user.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author thr
 * @since 2023-01-09
 */
@Service
public class ShopAuditLogServiceImpl extends BaseServiceImpl<ShopAuditLog> implements IShopAuditLogService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LoginContextUtils loginContextUtils;

    @Override
    public void add(HttpServletRequest req, ShopAuditLog shopAuditLog) {
        //拿到登录人的登录信息和id
        Employee loginUser = (Employee) loginContextUtils.getLoginUser(req);


        //构建默认参数
        shopAuditLog.setAuditTime(new Date());
        //目前没有做登录所以给一个假的
        //获取到审核店铺管理员的id
        shopAuditLog.setAuditId(loginUser.getId());

        //得到店铺
        Shop shop = shopMapper.findOne(shopAuditLog.getShopId());
        //系应该状态
        shop.setState(shopAuditLog.getState());
        //执行修改
        shopMapper.update(shop);
        //通过店铺查找用户
        Employee employee = employeeMapper.getEmpByShopId(shop.getId());
        //拿到管理员的id
        Long id = employee.getId();
        //发送邮件
        emailUtil.FromEmail("thr010420@outlook.com",id,shopAuditLog);

        super.add(shopAuditLog);
    }
}
